var askteacher_page = 1,askteacher_limit=10;
var askteacher_totalPage = 0;
var totalres=[];

$(".teachers").css('height',(window.innerHeight-60)+'px');

window.addEventListener('onDropdown', function(event){
    console.log('下接刷新：' );

    if(askteacher_page<askteacher_totalPage){
        askteacher_page++;

        loadData(this.askteacher_page,this.askteacher_limit);
    }else{
        console.log(askteacher_page)
        //  pop_up("没有更多了...")
    }
});
function todetail(teacherid) {
    server.loadindex('page/teacher/teacherdetail/teacherdetail','teachid='+teacherid,true);
};
//根据评分，得到小星星的数量
function scoreImage(score) {
    var himage="";
    if(score){
        if(score==0||(score>0&&score<1)){
            himage='<img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score==1||(score>1&&score<2)){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score==2||(score>2&&score<3)){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score==3||(score>3&&score<4)){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score==4||(score>4&&score<5)){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score>=5){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star">';
        }
    }else{
        himage='<img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
    }
    return himage;
};
function loadData(ipage,ilimit){
    var data={page:ipage,limit:ilimit}
    var serviceId = server.getQueryString("serviceId");
    if(serviceId)data.serviceId=serviceId;
    var teacherid = server.getQueryString("teacherid");
    if(teacherid)data.teacherid=teacherid;
    server.req('wechat/teacher/all',data,function (res) {
        totalres =totalres.concat(res.data);
        if(totalres)
            res.data=totalres;

        askteacher_totalPage= res.totalPage;
        if(res.code==0){
            //     res.data.forEach(function (item,index) {
            //         if(server.isEmpty(item.headimgurl))
            //             item.headimgurl = "img/icons/default.png";
            //     });
            res.data.forEach(function (item2,index2) {
                //转义在线状态
                if (item2.online) {
                    item2.stronline = 'img/teacher/online.png';

                } else {
                    item2.stronline = 'img/teacher/offline.png';
                }

                //根据得分多少显示小星星
                var himage = scoreImage(item2.evaluateScore);
                item2.himage = himage;
                if(!item2.description){
                    item2.description='老师有点懒，什么也没留下';
                }else if(item2.description.length>20){
                    item2.description=item2.description.substring(0,18)+"..";
                }
                if(!isNaN(item2.minprice)){
                    // console.log(item2.minprice);
                    item2.minprice=parseInt(item2.minprice);
                }

                // $("#himage").html(himage);
            })
            console.log("--------teachers---------");
            console.log(res)
            server.loadData(".teachers",res);
            //循环每个专家的特长
            var data = res.data;
            data.forEach(function (item,index) {
                if(server.isEmpty(item.headimgurl))
                    data[index].headimgurl = "img/icons/default.png";

                var sp; // = item.specialty;
                var json = [];
                var vsp; // = sp.split(";");


                // if(item.specialty&&item.specialty.indexOf(";")!=-1){
                //     vsp = item.specialty.split(";");
                // }else if(item.specialty&&item.specialty.indexOf(",")!=-1){
                //     vsp = item.specialty.split(",");
                // }else{
                //     vsp = item.specialty||'';
                // }
                if(item.skills&&item.skills.indexOf(";")!=-1){
                    vsp = item.skills.split(";");
                }else if(item.skills&&item.skills.indexOf(",")!=-1){
                    vsp = item.skills.split(",");
                }else{
                    vsp = item.skills||'';
                }

                // console.log(index);
                if(vsp) {
                    var t=vsp.length;
                    if(t>3)t=3;
                    for (var i = 0; i < t; i++) {
                        var jsp = {'spname':vsp[i]};
                        json.push(jsp);
                    }
                }
                item.vsp = json;

                // console.log("#teachers-item-row3-item-btn"+item.id);
                server.loadData("#teachers-item-row3-item-btn"+item.id,data[index]);
            });


            // server.loadData(".teachers",res);


        }
    })
};
$(function () {
    loadData(askteacher_page,askteacher_limit);
})